package com.ibm.cps.sql.expression.function.meta;

import java.util.Arrays;

public class FunctionArgChecker {
	public static final int INT_CONST = 0x1;
	public static final int DOUBLE_CONST = 0x2;
	public static final int NUMBER_CONST = INT_CONST | DOUBLE_CONST;
	public static final int STRING_CONST = 0x4;
	public static final int FIELD = 0x8;
	
	private final ArgType[] validTypes;
	
	public FunctionArgChecker(ArgType... validTypes){
		this.validTypes = validTypes;
	}
	
	public ArgType[] getValidTypes() {
		return validTypes;
	}

	public String checkArgTypes(ArgType[] types){
		String failureReason = null;
		if(validTypes.length != types.length){
			failureReason = "argument count mismatch: "+ types.length + " arguments provided, " + validTypes.length + " arguments expected";
			return failureReason;
		}
		for (int i = 0; i < types.length; i++) {
			ArgType expected = validTypes[i];
			ArgType provided = types[i];
			if((expected.getFlag() & provided.getFlag()) == 0){
				failureReason = "argument type mismatch, provided: " + Arrays.toString(types) + ", expected: " + Arrays.toString(validTypes);
				return failureReason;
			}
		}
		return failureReason = null;
	}
	
	public static enum ArgType{
		FIELD(0x4), CONST(0x3), FIELD_OR_CONST(0x7), NUMBER_CONST(0x2), STRING_CONST(0x1);
		
		private final int flags;

		private ArgType(int flags) {
			this.flags = flags;
		}

		public int getFlag() {
			return flags;
		}

	}
}
